package cn.dnf.org.controller;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.util.ExcelUtils;
import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.PageList;
import cn.dnf.org.domain.ShopLog;
import cn.dnf.org.dto.ShopDto;
import cn.dnf.org.service.IShopService;
import cn.dnf.org.domain.Shop;
import cn.dnf.org.query.ShopQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return JsonResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return JsonResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return JsonResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.pageQuery(query);
    }


    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e) { //业务异常
            e.printStackTrace();
            //代码问题（其他不可抗拒因素）
            return JsonResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            //代码问题（其他不可抗拒因素）
            return JsonResult.me().setMsg("系统异常,稍后重试");
        }
    }

    @PostMapping("/audit/pass")
    @ApiOperation(value="审核通过")
    public JsonResult pass(@RequestBody ShopLog log){
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }

    @PostMapping("/audit/reject")
    @ApiOperation(value="驳回")
    public JsonResult reject(@RequestBody ShopLog log){
        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            //查询所有shop数据
            List<Shop> list = shopService.loadAll();
            //调用工具类
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xls",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 导入  ，调用工具类  MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass

            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            // 保存到数据库表中
            list.forEach(System.out::println);
            shopService.patchInsert(list);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * domain,pojo:实体类
     * query 做查询参数使用
     * dto: 接收参数或者传参使用
     * @return
     */

@GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }

}
